package cn.hnu.qq.frame;

import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import org.apache.log4j.Logger;

import cn.hnu.qq.client.controller.CenterController;
import cn.hnu.qq.client.controller.ListenClientMessageThread;
import cn.hnu.qq.client.controller.ListenServerMessageThread;
import cn.hnu.qq.client.controller.SingleChartController;
import cn.hnu.qq.client.event.MessageEvent;
import cn.hnu.qq.client.listener.MessageListener;
import cn.hnu.qq.client.panel.DefaultModelChartPanel;
import cn.hnu.qq.entity.TaskEvent;
import cn.hnu.qq.entity.User;
import cn.hnu.qq.entity.message.ChartMessage;
import cn.hnu.qq.entity.message.Message;
import cn.hnu.qq.global.MessageType;

public class SingleChartDialog extends JDialog implements ActionListener,MessageListener{

	private SingleChartController controller = SingleChartController.getInstance();
	
	private JTabbedPane tabbedPane;
	
	private Map<String,Integer> openedPanel = new HashMap<String,Integer>();
	
	private static SingleChartDialog instance = new SingleChartDialog();
	
	int count = 0;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			SingleChartDialog dialog = new SingleChartDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	private SingleChartDialog() {
		try {
			setIconImage(ImageIO.read(new File("src/main/java/image/xiaohui.jpg")));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
		Logger.getLogger(SingleChartDialog.class).debug("构造SingleChartDialog");
		setBounds(100, 100, 449, 303);
		getContentPane().setLayout(null);
		
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(10, 10, 421, 249);
		getContentPane().add(tabbedPane);
		ListenClientMessageThread.getInstance().addMessageListener(this);
		ListenServerMessageThread.getInstance().addMessageListener(this);
	}
	
	public static SingleChartDialog getInstance(){
		return instance;
	}
	
	public void refreshPanel(JPanel panel){
		this.getRootPane().removeAll();
		this.setSize(panel.getWidth()+10, panel.getHeight()+10);
		this.getRootPane().add(panel);
		this.validate();
	}
	/**
	 * 如果新建一个tab 将返回它的引用 否则为null
	 * @param user
	 * @return
	 */
	public DefaultModelChartPanel createChartTab(final User user){
		DefaultModelChartPanel dp = null;
		if(openedPanel.get(user.getAcount()) != null){
			int i = openedPanel.get(user.getAcount());
			tabbedPane.setSelectedIndex(i);
			controller.queryChartIpInfo(user);
			return (DefaultModelChartPanel) tabbedPane.getSelectedComponent();
//			controller.getSingDialog().refreshPanel(this);
		}else{
			Logger.getLogger(CenterController.class).debug(("进入createChartTab else"));
			dp = new DefaultModelChartPanel();
			dp.setOwnner(user);
			tabbedPane.add(dp,count);
			tabbedPane.setTitleAt(count, user.getAcount());
			tabbedPane.setSize(dp.getWidth()+10,dp.getHeight()+25);
			openedPanel.put(user.getAcount(), count);
			tabbedPane.setSelectedIndex(count++);
			EventQueue.invokeLater(new Runnable() {
				public void run() {
					controller.queryChartIpInfo(user);
				}
			});
//			TODO 这里离开一个对话框
//			
//			SystemPanel system = new SystemPanel(user.getAcount());
//			system.addActionListener(this);
//			tabbedPane.setComponentAt(count++, system);
			this.setSize(tabbedPane.getWidth()+20, tabbedPane.getHeight()+55);
			validate();
		}
		return dp;
	}

	public void actionPerformed(ActionEvent e) {
//		tabbedPane.remove(tabbedPane.getSelectedIndex());
//		openedPanel.remove(((SystemPanel)e.getSource()).getTitle());
//		count--;
	}

	public void messageArrived(MessageEvent event) {
		Message message = event.getSource();
		if(message.getMessageType() == MessageType.CHART){
			ChartMessage cm = (ChartMessage) message;
			if(openedPanel.get(cm.getAccount()) == null){
				DefaultModelChartPanel dmcp = createChartTab(controller.getCurUser().findFriend(cm.getAccount()));
				dmcp.setOwnner(controller.getCurUser().findFriend(cm.getAccount()));
				dmcp.insertMessage(cm);
			}else{
				tabbedPane.setSelectedIndex(openedPanel.get(cm.getAccount()));
			}
		}
//		if(message.getMessageType() == MessageType.LEAVE_WORDS){
//			TaskEvent task = (TaskEvent) message;
//			if(openedPanel.get(task.getMaker().getAcount()) == null){
//				DefaultModelChartPanel dmcp = createChartTab(controller.getCurUser().findFriend(task.getMaker().getAcount()));
//				dmcp.insertMessage(task);
//			}else{
//				tabbedPane.setSelectedIndex(openedPanel.get(task.getMaker().getAcount()));
//				DefaultModelChartPanel dmp = (DefaultModelChartPanel) tabbedPane.getSelectedComponent();
//				dmp.insertMessage(task);
//			}
//		}
	}

	public void closeChartTab(User user) {
		Integer  index = openedPanel.get(user.getAcount());
		if(index != null){
			tabbedPane.removeTabAt(index);
			openedPanel.remove(user.getAcount());
			count--;
			if(count == 0){
				this.dispose();
			}
		}
		//TODO 这里需要解决垃圾回收，即解决掉不需要的监听
		//tabbedPane.getTabComponentAt(index);
	}
	
}
